package com.example.scheduled.task;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableAsync
public class AsyncTask {

    @Scheduled(cron = "*/10 * * * * *")
    @Async("asyncScheduledTask")
    public void runTask1() {
        System.out.println("task1 start...");
        try {
            System.out.println("task1执行中：" + Thread.currentThread().getName());
            // 模拟一个耗时的任务
            Thread.sleep(5000); // 5秒
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        System.out.println("task1 end...");
    }

    @Scheduled(cron = "*/10 * * * * *")
    @Async("asyncScheduledTask")
    public void runTask2() {
        System.out.println("task2 start...");
        try {
            System.out.println("task2执行中：" + Thread.currentThread().getName());
            // 模拟一个耗时的任务
            Thread.sleep(3000); // 3秒
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        System.out.println("task2 end...");
    }
}